﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FontMaster
{
    public partial class frmOutput : Form
    {
        public FontOutput fontOutput;

        public frmOutput(FontOutput fontOutput)
        {
            this.fontOutput = fontOutput;

            InitializeComponent();

            checkBoxClng.Checked = fontOutput.Clng.Enable;
            textBoxClngFile.Text = fontOutput.Clng.FileName;
            checkBoxClngHeader.Checked = fontOutput.Clng.ContentCheck(FontFile.CONTENT_FONT_HEADER);
            checkBoxClngSection.Checked = fontOutput.Clng.ContentCheck(FontFile.CONTENT_FONT_SECTION);
            checkBoxClngIndex.Checked = fontOutput.Clng.ContentCheck(FontFile.CONTENT_FONT_INDEX);
            checkBoxClngData.Checked = fontOutput.Clng.ContentCheck(FontFile.CONTENT_FONT_DATA);

            checkBoxFnt.Checked = fontOutput.Fnt.Enable;
            textBoxFntFile.Text = fontOutput.Fnt.FileName;
            checkBoxFntHeader.Checked = fontOutput.Fnt.ContentCheck(FontFile.CONTENT_FONT_HEADER);
            checkBoxFntSection.Checked = fontOutput.Fnt.ContentCheck(FontFile.CONTENT_FONT_SECTION);
            checkBoxFntIndex.Checked = fontOutput.Fnt.ContentCheck(FontFile.CONTENT_FONT_INDEX);
            checkBoxFntData.Checked = fontOutput.Fnt.ContentCheck(FontFile.CONTENT_FONT_DATA);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            FontOutput fontOutputTmp = new FontOutput();

            fontOutputTmp.Clng.Enable = checkBoxClng.Checked;
            fontOutputTmp.Clng.FileName = textBoxClngFile.Text;
            fontOutputTmp.Clng.ContentMark(FontFile.CONTENT_FONT_HEADER, checkBoxClngHeader.Checked);
            fontOutputTmp.Clng.ContentMark(FontFile.CONTENT_FONT_SECTION, checkBoxClngSection.Checked);
            fontOutputTmp.Clng.ContentMark(FontFile.CONTENT_FONT_INDEX, checkBoxClngIndex.Checked);
            fontOutputTmp.Clng.ContentMark(FontFile.CONTENT_FONT_DATA, checkBoxClngData.Checked);

            fontOutputTmp.Fnt.Enable = checkBoxFnt.Checked;
            fontOutputTmp.Fnt.FileName = textBoxFntFile.Text;
            fontOutputTmp.Fnt.ContentMark(FontFile.CONTENT_FONT_HEADER, checkBoxFntHeader.Checked);
            fontOutputTmp.Fnt.ContentMark(FontFile.CONTENT_FONT_SECTION, checkBoxFntSection.Checked);
            fontOutputTmp.Fnt.ContentMark(FontFile.CONTENT_FONT_INDEX, checkBoxFntIndex.Checked);
            fontOutputTmp.Fnt.ContentMark(FontFile.CONTENT_FONT_DATA, checkBoxFntData.Checked);

            if (!fontOutputTmp.CheckValid())
            {
                MessageBoxEx.Show("请检查设置！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            fontOutput = fontOutputTmp;

            this.DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void buttonClngFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter = "C语言文件(*.c)|*.c";
            dlg.FileName = textBoxClngFile.Text;
            dlg.OverwritePrompt = false;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBoxClngFile.Text = dlg.FileName;
            }
        }

        private void buttonFntFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter = "FNT字库文件(*.fnt)|*.fnt|所有文件(*.*)|*.*";
            dlg.FileName = textBoxFntFile.Text;
            dlg.OverwritePrompt = false;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                textBoxFntFile.Text = dlg.FileName;
            }
        }

        private void checkBoxClng_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxClng.Enabled = checkBoxClng.Checked;
        }

        private void checkBoxFnt_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxFnt.Enabled = checkBoxFnt.Checked;
        }
    }

    public class FontFile
    {
        public static int CONTENT_FONT_HEADER = 0x01;
        public static int CONTENT_FONT_SECTION = 0x02;
        public static int CONTENT_FONT_INDEX = 0x04;
        public static int CONTENT_FONT_DATA = 0x08;
        public static int CONTENT_FONT_ALL = CONTENT_FONT_HEADER | CONTENT_FONT_SECTION | CONTENT_FONT_INDEX | CONTENT_FONT_DATA;

        public string FileName;
        public bool Enable;
        int Content;

        public FontFile()
        {
            Content = CONTENT_FONT_ALL;
        }

        public override string ToString()
        {
            return Enable.ToString() + ",\"" + FileName + "\"";
        }

        public bool CheckValid()
        {
            return ((FileName.Length > 0) && (Content != 0));
        }

        public void ContentMark(int mask, bool mark)
        {
            if (mark)
            {
                Content |= mask;
            }
            else
            {
                Content &= ~mask;
            }
        }

        public bool ContentCheck(int mask)
        {
            return ((Content & mask) != 0);
        }
    }

    public class FontOutput
    {
        public FontFile Clng { get; set; }
        public FontFile Fnt { get; set; }

        public FontOutput()
        {
            Clng = new FontFile();
            Fnt = new FontFile();
        }

        public override string ToString()
        {
            if (Clng.Enable && Fnt.Enable)
            {
                return "C语言和FNT字库文件";
            }

            if (Clng.Enable)
            {
                return "C语言文件";
            }

            if (Fnt.Enable)
            {
                return "FNT字库文件";
            }

            return "未设置";
        }

        public bool CheckValid()
        {
            if ((!Clng.Enable) && (!Fnt.Enable))
            {
                return false;
            }

            if (Clng.Enable && !Clng.CheckValid())
            {
                return false;
            }

            if (Fnt.Enable && !Fnt.CheckValid())
            {
                return false;
            }

            return true;
        }
    }
}
